package qianting.news.common.utils;

import qianting.news.common.entity.AuthUser;

public class AppThreadLocalUtil {

    private final static ThreadLocal<AuthUser> USER_THREAD_LOCAL = new ThreadLocal<>();
    private final static InheritableThreadLocal<AuthUser> USER_THREAD_LOCAL_INHERIT=new InheritableThreadLocal<>();

    /**
     * 添加用户
     * @param user
     */
    public static void  setUser(AuthUser user){
        USER_THREAD_LOCAL.set(user);
        USER_THREAD_LOCAL_INHERIT.set(user);
    }

    /**
     * 获取用户
     */
    public static AuthUser getUser(){
        return USER_THREAD_LOCAL.get()!=null?USER_THREAD_LOCAL.get():USER_THREAD_LOCAL_INHERIT.get();
    }

    /**
     * 清理用户
     */
    public static void clear(){
        USER_THREAD_LOCAL.remove();
        USER_THREAD_LOCAL_INHERIT.remove();
    }
}